package com.wltt.product.frfront.config.javafx;

import com.wltt.product.frfront.config.exception.RobotException;
import com.wltt.product.frfront.config.javafx.bo.RefTypeBO;
import com.wltt.product.frfront.config.javafx.utils.AlertUtil;
import com.wltt.product.frfront.executors.JointMoveForJavaFxExecutor;
import com.wltt.product.frfront.executors.PoseMoveForJavaFxExecutor;
import com.wltt.product.frfront.holder.JavafxHolder;
import com.wltt.product.frfront.holder.RobotHolder;
import fairino.ROBOT_STATE_PKG;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.input.InputEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.util.Duration;
import org.springframework.core.io.ClassPathResource;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.PowerSource;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;

public class SettingController implements Initializable {

    private static final String IPV4_PATTERN =
            "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";

    @FXML
    private ComboBox<Integer> toolCoordinateNumComboBox;

    @FXML
    private ComboBox<Integer> weaveNumComboBox;

    @FXML
    private ComboBox<Integer> arcNumComboBox;

    @FXML
    private ComboBox<Integer> workpieceCoordinateNumComboBox;

    @FXML
    private Label maxStepLabel;

    @FXML
    private Slider maxStepSlider;

    @FXML
    private ComboBox<RefTypeBO> refTypeComboBox;
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        ObservableList<Integer> options1 = FXCollections.observableArrayList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14);
        toolCoordinateNumComboBox.setItems(options1);
        toolCoordinateNumComboBox.getSelectionModel().select(RobotHolder.getRobot().getRobotPO().getToolCoordinateNum());
        toolCoordinateNumComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
            RobotHolder.getRobot().getRobotPO().setToolCoordinateNum(newValue);
            RobotHolder.getRobot().save();
        });

        ObservableList<Integer> options2 = FXCollections.observableArrayList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14);
        workpieceCoordinateNumComboBox.setItems(options2);
        workpieceCoordinateNumComboBox.getSelectionModel().select(RobotHolder.getRobot().getRobotPO().getWorkpieceCoordinateNum());
        workpieceCoordinateNumComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
            RobotHolder.getRobot().getRobotPO().setWorkpieceCoordinateNum(newValue);
            RobotHolder.getRobot().save();
        });

        ObservableList<Integer> options3 = FXCollections.observableArrayList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14);
        weaveNumComboBox.setItems(options3);
        weaveNumComboBox.getSelectionModel().select(RobotHolder.getRobot().getRobotPO().getWeaveConfigNum());
        weaveNumComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
            RobotHolder.getRobot().getRobotPO().setWeaveConfigNum(newValue);
            RobotHolder.getRobot().save();
        });

        ObservableList<Integer> options5 = FXCollections.observableArrayList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14);
        arcNumComboBox.setItems(options5);
        arcNumComboBox.getSelectionModel().select(RobotHolder.getRobot().getRobotPO().getArcConfigNum());
        arcNumComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
            RobotHolder.getRobot().getRobotPO().setArcConfigNum(newValue);
            RobotHolder.getRobot().save();
        });

        ObservableList<RefTypeBO> options4 = FXCollections.observableArrayList(new RefTypeBO(2, "基坐标系"),
                new RefTypeBO(4, "工具坐标系"), new RefTypeBO(8, "工件坐标系"));
        refTypeComboBox.setItems(options4);
        refTypeComboBox.getSelectionModel().select(0);

        int maxValue = (int) maxStepSlider.getValue();
        maxStepLabel.setText(String.valueOf(maxValue));
        RobotHolder.getRobot().getRobotPO().setMaxStep(maxValue);
        maxStepSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
            int value = newValue.intValue();
            RobotHolder.getRobot().getRobotPO().setMaxStep(value);
            RobotHolder.getRobot().save();
            maxStepLabel.setText(String.valueOf(value));
        });
    }

    @FXML
    public void forwardWireFeed(InputEvent event) {
        Platform.runLater(() -> {
            if (RobotHolder.getIsError()) {
                AlertUtil.alert(Alert.AlertType.WARNING, "警告", "存在异常信息", "请先清除当前机械臂的异常信息再尝试！");
            }

            if (RobotHolder.getIsError()) {
                AlertUtil.alert(Alert.AlertType.WARNING, "警告", "存在异常信息", "请先清除当前机械臂的异常信息再尝试！");
            }
            try {
                RobotHolder.getRobot().SetForwardWireFeed(0, 1);
            } catch (RobotException e) {
                AlertUtil.alert(Alert.AlertType.ERROR, "错误", "送丝失败", e.getMessage());
            }
        });
    }

    @FXML
    public void stopForwardWireFeed(InputEvent event) {
        try {
            RobotHolder.getRobot().SetForwardWireFeed(0, 0);
        } catch (Exception e) {
        }
    }

    @FXML
    public void reverseWireFeed(InputEvent event) {
        Platform.runLater(() -> {
            if (RobotHolder.getIsError()) {
                AlertUtil.alert(Alert.AlertType.WARNING, "警告", "存在异常信息", "请先清除当前机械臂的异常信息再尝试！");
            }
            try {
                RobotHolder.getRobot().SetReverseWireFeed(0, 1);
            } catch (RobotException e) {
                AlertUtil.alert(Alert.AlertType.ERROR, "错误", "退丝失败", e.getMessage());
            }
        });
    }

    @FXML
    public void stopReverseWireFeed(InputEvent event) {
        try {
            RobotHolder.getRobot().SetReverseWireFeed(0, 0);
        } catch (Exception e) {
        }
    }

    @FXML
    public void airCheck(ActionEvent event) {
        if (RobotHolder.getIsError()) {
            AlertUtil.alert(Alert.AlertType.WARNING, "警告", "存在异常信息", "请先清除当前机械臂的异常信息再尝试！");
        }
        try {
            RobotHolder.getRobot().SetAspirated(0, 1);
            RobotHolder.getRobot().Sleep(500);
            RobotHolder.getRobot().SetAspirated(0, 0);
        } catch (RobotException e) {
            AlertUtil.alert(Alert.AlertType.ERROR, "错误", "气体检测失败", e.getMessage());
        }
    }

    @FXML
    public void poseMove(InputEvent event) {
        if (RobotHolder.getIsError()) {
            AlertUtil.alert(Alert.AlertType.WARNING, "警告", "存在异常信息", "请先清除当前机械臂的异常信息再尝试！");
        }
        RobotButton source = (RobotButton)event.getSource();
        RefTypeBO refTypeBO = refTypeComboBox.getValue();
        PoseMoveForJavaFxExecutor executor = new PoseMoveForJavaFxExecutor(refTypeBO.getTypeId(), source.getNb(), source.getDir());
        Platform.runLater(executor);
    }

    @FXML
    public void JointMove(InputEvent event) {
        if (RobotHolder.getIsError()) {
            AlertUtil.alert(Alert.AlertType.WARNING, "警告", "存在异常信息", "请先清除当前机械臂的异常信息再尝试！");
        }
        RobotButton source = (RobotButton)event.getSource();
        JointMoveForJavaFxExecutor executor = new JointMoveForJavaFxExecutor(source.getNb(), source.getDir());
        Platform.runLater(executor);
    }

    @FXML
    public void moveStop(InputEvent event) {
        try {
            RobotHolder.getRobot().ImmStopJOG();
        } catch (Exception e) {
        } finally {
            RobotHolder.setRobotNoMoving();
        }

    }
}